package com.punch.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.order.SettlementQueryInfo;
import com.punch.common.tools.BasePage;
import com.punch.common.vo.course.MaterialSettlementVO;
import com.punch.common.vo.order.SettlementPageVO;
import com.punch.order.exception.OrderExceptionEnum;
import com.punch.order.mapper.MaterialSettlementMapper;
import com.punch.order.model.MaterialSettlement;
import com.punch.order.service.MaterialSettlementService;
import org.springframework.stereotype.Service;

/**
 * 教具结算信息实现层
 */
@Service
public class MaterialSettlementServiceImpl extends ServiceImpl<MaterialSettlementMapper, MaterialSettlement> implements MaterialSettlementService {
    /**
     * 教具结算列表
     *
     * @param settlementQueryInfo 结算查询信息
     * @return SettlementPageVO
     */
    @Override
    public PageInfo<SettlementPageVO> listMaterialSettlement(SettlementQueryInfo settlementQueryInfo) {
        return BasePage.queryBasePage(settlementQueryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> this.getBaseMapper().queryList(params));
    }

    /**
     * 教具结算详情
     *
     * @param settlementId 结算id
     * @return OrderMaterialSettlementVO
     */
    @Override
    public MaterialSettlementVO getMaterialSettlement(Long settlementId) {
        MaterialSettlementVO materialSettlementVO = new MaterialSettlementVO();
        MaterialSettlement materialSettlement = getById(settlementId);
        if (materialSettlement == null) {
            throw new BusinessException(OrderExceptionEnum.ORDER_NOT_EXIST);
        }
        materialSettlementVO.setClassName(materialSettlement.getCourseClassName());
        materialSettlementVO.setSchoolName(materialSettlement.getSchoolName());
        materialSettlementVO.setCourseMaterialName(materialSettlement.getMaterialName());
        materialSettlementVO.setCourseMaterialPrice(materialSettlement.getPrice());
        materialSettlementVO.setBuyCount(materialSettlement.getBuyCount());
        materialSettlementVO.setSettlementMaterialAmount(materialSettlement.getSettlementAmount());
        return materialSettlementVO;
    }

    /**
     * 教具结算修改状态 结算状态 0-待确认 1-已确认 2-已打款
     *
     * @param settlementId     结算id
     * @param settlementStatus
     * @return settlementId
     */
    @Override
    public Long updateMaterialSettlementStatus(Long settlementId, Integer settlementStatus) {
        MaterialSettlement materialSettlementEX = getById(settlementId);
        if (materialSettlementEX == null) {
            throw new BusinessException(OrderExceptionEnum.MATERIAL_SETTLEMENT_NOT_EXIST);
        }
        Integer currentStatus = materialSettlementEX.getSettlementStatus();
        //待确认-->已确认
        if (settlementStatus == 1) {
            //判断状态是否是待确认
            if (currentStatus != 0) {
                throw new BusinessException(OrderExceptionEnum.MATERIAL_SETTLEMENT_STATUS_ERROR);
            }
        }
        //已确认-->已打款
        if (settlementStatus == 2) {
            //判断状态是否是待确认
            if (currentStatus != 1) {
                throw new BusinessException(OrderExceptionEnum.MATERIAL_SETTLEMENT_STATUS_ERROR);
            }
        }
        MaterialSettlement materialSettlement = new MaterialSettlement();
        materialSettlement.setId(settlementId);
        materialSettlement.setSettlementStatus(settlementStatus);
        updateById(materialSettlement);
        return settlementId;
    }
}
